package ${package.Controller};

import ${package.Service}.${table.serviceName};
import ${package.Entity}.${entity};
<#if restControllerStyle>
import org.springframework.web.bind.annotation.*;
<#else>
import org.springframework.stereotype.Controller;
</#if>
<#if superControllerClassPackage??>
import ${superControllerClassPackage};
</#if>
import org.springframework.beans.factory.annotation.Autowired;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

/**
 * <p>
 * ${table.comment!} 管理入口
 * </p>
 *
 * @author ${author}
 * @since ${date}
 */
@Slf4j
<#if restControllerStyle>
@RestController
<#else>
@RestController
</#if>
@RequestMapping("/<#if controllerMappingHyphenStyle??>${controllerMappingHyphen}<#else>${table.entityPath}</#if>")
@Api(value = "${table.controllerName}", tags = "${table.comment!}")
<#if kotlin>
class ${table.controllerName}<#if superControllerClass??> : ${superControllerClass}()</#if>
<#else>
    <#if superControllerClass??>
public class ${table.controllerName} extends ${superControllerClass} {
    <#else>
public class ${table.controllerName} {
    </#if>

    @Autowired
    private ${table.serviceName} service;


    @ApiOperation(value = "${table.comment!}")
    @PostMapping("/addOrUpdate")
    public RestResponse addOrUpdate(@RequestBody ${entity} data) {
        try {
                if (data.getId() == null) {
                    return service.add(data);
                } else {
                    return service.update(data);
                }
        } catch (Exception e) {
            log.error("添加或修改${table.comment!}异常", e);
            return RestResponse.error(e.getMessage());
        }
    }

    @ApiOperation(value = "删除或批量${table.comment!}")
    @PostMapping("/delete")
    public RestResponse delete(@RequestBody IdParamBO req) {
        try {
            return service.delete(req);
        } catch (Exception e) {
            log.error("删除${table.comment!}异常", e);
            return RestResponse.error(e.getMessage());
        }
    }

    @ApiOperation(value = "查询${table.comment!}详情")
    @GetMapping("/detail")
    public RestResponse detail(@RequestParam Long id) {
        try {
            return service.detail(id);
        } catch (Exception e) {
            log.error("查询${table.comment!}详情异常", e);
            return RestResponse.error(e.getMessage());
        }
    }

    @ApiOperation(value = "分页查询${table.comment!}")
    @PostMapping("/listPage")
    public RestResponse list(@RequestBody(required = false) ${entity}QueryBO req) {
        try {
            return service.listPage(req);
        } catch (Exception e) {
            log.error("分页查询${table.comment!}异常", e);
            return RestResponse.error(e.getMessage());
        }
    }
}
</#if>
